package com.example.quasarpractise;/*
    @ClassName test
    @Description 测试接口
    @Author Donkiss
    @Date 2021/12/13 1:46
    @Detail：
*/

import co.paralleluniverse.fibers.Fiber;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/testapi")
public class test {

    private AtomicInteger h = new AtomicInteger(0);

    @GetMapping("/test")
    public String test() {
        LinkedBlockingQueue<Fiber<Integer>> queue = new LinkedBlockingQueue<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 0; i < 10000; i++) {
            Fiber<Integer> fibers = new Fiber<>(() -> {
                int tmp = h.addAndGet(1);
                return tmp;
            });
            fibers.start();
            queue.add(fibers);
        }
        while (!queue.isEmpty()) {
            try {
                Fiber<Integer> fiber = queue.take();
                System.out.println("out-" + fiber.get() + "-" + LocalDateTime.now().format(formatter));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        return "测试结束";
    }
}
